package com.ruoyi.web.controller.kpl.other;

import lombok.SneakyThrows;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DateUtils {
    public static String now(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return sdf.format(new Date());
    }

    public static String now(String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);

        return sdf.format(new Date());
    }

    public static Date getDateFromStr(String date,String pattern) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(date);
    }

    /**
     * 获取指定日期的 最近上一个工作日
     * @param inputDate
     * @return
     */
    public static String lastWorkDate(String inputDate){
        // 1. 定义指定日期字符串格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 2. 解析日期并获取前一天
        LocalDate date = LocalDate.parse(inputDate, formatter);

        // 3. 计算上一个工作日
        LocalDate previousWorkingDate = calculatePreviousWorkingDay(date);

        // 3. 格式化输出结果
        String result = previousWorkingDate.format(formatter);

        return result;
    }

    public static LocalDate calculatePreviousWorkingDay(LocalDate date) {
        // 先获取前一天
        LocalDate previousDate = date.minusDays(1);

        // 循环调整直到遇到工作日（周一至周五）
        while (previousDate.getDayOfWeek() == DayOfWeek.SATURDAY ||
                previousDate.getDayOfWeek() == DayOfWeek.SUNDAY) {
            previousDate = previousDate.minusDays(1);
        }

        // 获取当前时间（不含日期）
        LocalTime now = LocalTime.now();

        // 定义目标时间 9:30
        LocalTime targetTime = LocalTime.of(9, 30);

        // 判断是否在 9:30 之前
        /*boolean isBefore930 = now.isBefore(targetTime);

        if(isBefore930){
            previousDate = previousDate.minusDays(1);//如果是9点30分之前 再减去一天
        }*/

        return previousDate;
    }

    /**
     * 获取指定日期到今天的工作日
     * @param startDateStr
     * @return
     */
    public static List<String> getWorkDays(String startDateStr){
        // 指定起始日期（可按需修改或通过参数传入）

        // 定义日期格式器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 解析起始日期和结束日期（今天）
        LocalDate startDate = LocalDate.parse(startDateStr, formatter);
        LocalDate endDate = LocalDate.now();

        // 存储工作日的列表
        List<LocalDate> workingDays = new ArrayList<>();

        // 遍历日期范围
        LocalDate currentDate = startDate;
        while (!currentDate.isAfter(endDate)) {
            // 检查是否为工作日（周一至周五）
            if (currentDate.getDayOfWeek() != DayOfWeek.SATURDAY
                    && currentDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
                workingDays.add(currentDate);
            }
            currentDate = currentDate.plusDays(1);
        }

        List<String> days = new ArrayList<>();

        List<String> holidays = new ArrayList<>();

        holidays.add("2024-01-01");
        holidays.add("2024-02-10");
        holidays.add("2024-02-11");
        holidays.add("2024-02-12");
        holidays.add("2024-02-13");
        holidays.add("2024-02-14");
        holidays.add("2024-02-15");
        holidays.add("2024-02-16");
        holidays.add("2024-02-17");
        holidays.add("2024-02-04");
        holidays.add("2024-02-18");
        holidays.add("2024-04-04");
        holidays.add("2024-04-05");
        holidays.add("2024-04-06");
        holidays.add("2024-04-07");
        holidays.add("2024-05-01");
        holidays.add("2024-05-02");
        holidays.add("2024-05-03");
        holidays.add("2024-05-04");
        holidays.add("2024-05-05");
        holidays.add("2024-04-28");
        holidays.add("2024-05-11");
        holidays.add("2024-06-08");
        holidays.add("2024-06-09");
        holidays.add("2024-06-10");
        holidays.add("2024-09-15");
        holidays.add("2024-09-16");
        holidays.add("2024-09-17");
        holidays.add("2024-09-14");
        holidays.add("2024-10-01");
        holidays.add("2024-10-02");
        holidays.add("2024-10-03");
        holidays.add("2024-10-04");
        holidays.add("2024-10-05");
        holidays.add("2024-10-06");
        holidays.add("2024-10-07");

        holidays.add("2025-01-01");
        holidays.add("2025-01-28");
        holidays.add("2025-01-29");
        holidays.add("2025-01-30");
        holidays.add("2025-01-31");
        holidays.add("2025-02-01");
        holidays.add("2025-02-02");
        holidays.add("2025-02-03");
        holidays.add("2025-02-04");
        holidays.add("2025-04-04");
        holidays.add("2025-04-05");
        holidays.add("2025-04-06");
        holidays.add("2025-05-01");
        holidays.add("2025-05-02");
        holidays.add("2025-05-03");
        holidays.add("2025-05-04");
        holidays.add("2025-05-05");
        holidays.add("2025-05-31");
        holidays.add("2025-06-01");
        holidays.add("2025-06-02");
        holidays.add("2025-10-01");
        holidays.add("2025-10-02");
        holidays.add("2025-10-03");
        holidays.add("2025-10-04");
        holidays.add("2025-10-05");
        holidays.add("2025-10-06");
        holidays.add("2025-10-07");
        holidays.add("2025-10-08");

        // 格式化输出结果
        for (LocalDate date : workingDays) {
            if(holidays.contains(date.toString())){
                continue;
            }
            days.add(date.format(formatter));
        }
        return days;

    }



    public static void main(String[] args) {
        List<String> workDays = getWorkDays("2025-07-15");
        workDays.forEach(System.out::println);
    }

    public static String getDateStr(Date date, String pattern) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }



    @SneakyThrows
    public static Date parseDate(String date,String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(date);
    }

    public static String formatDate(Date date,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }



}
